package gui.batches;

import java.util.Stack;

import core.exception.ModelException;

public class CommandHistory {
	private Stack<Command> _undos;;
	private Stack<Command> _redos;
	
	public CommandHistory(){
		_undos = new Stack<Command>();
		_redos = new Stack<Command>();
	}
	
	/**
	 * Return true if you can undo
	 * @return true if you can undo
	 */
	public boolean canUndo(){
		return !_undos.isEmpty();
	}
	
	/**
	 * Return true if you can redo
	 * @return true if you can redo
	 */
	public boolean canRedo(){
		return !_redos.isEmpty();
	}
	
	/**
	 * Add the command to the history. This method assumes the command
	 * has already been executed.
	 * @param c
	 */
	public void addCommandToHistory(Command c){
		_undos.push(c);
		_redos.clear();
	}
	
	/**
	 * Undo the action. The ModelException is whatever might have been thrown
	 * by undoing the action. 
	 * 
	 * @pre canUndo() must return true
	 * @throws ModelException
	 */
	public void undo() throws ModelException{
		assert(canUndo());
		Command c = _undos.pop();
		_redos.push(c);
		c.undo();
	}
	
	/**
	 * Redo the action. The Model exception is whatever was thrown by redoing the
	 * action.
	 * 
	 * @pre canRedo() must return true
	 * @throws ModelException
	 */
	public void redo() throws ModelException{
		assert(canRedo());
		Command c = _redos.pop();
		_undos.push(c);
		c.execute();
	}
}
